<?php
class Api_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
	
	function listContents($dataJson)
	{
		$id = @$dataJson->id;
		$contentId = @$dataJson->contentId;
		$limit = (@$dataJson->params->limit == 0 ? 100:$dataJson->params->limit);
		$offset = @$dataJson->params->offset;
		switch(@$dataJson->params->contentType):
			case 'FS':
				$dataResult = $this->getFullSong($id,$contentId,$limit,$offset);
			break;
			case 'MV':
				$dataResult = $this->getMusicVideo($id,$contentId,$limit,$offset);
			break;						
			default:
				$dataResult = $this->getFullSongAndMusicVideo($id,$contentId,$limit,$offset);
		endswitch;
		return $dataResult;
	}
	
	private function getFullSong($id = null, $contentId = null, $limit = null, $offset = null)
	{
		/*Initail Setup*/
		$allData = array(
			'jsonrpc' => '2.0',
			'id' => $id,
			'allItems' => 	$this->DB_LIVE->from('song_desc as t')->where(array('t.language_id' => 1))->
							join('song as p','p.song_id=t.song_id','INNER')->
							join('artist_song as j','j.song_id=t.song_id','INNER')->
							join('artist_desc as k',"k.artist_id=j.artist_id AND k.language_id='1'",'INNER')->
							join('artist as q','q.artist_id=k.artist_id','INNER')->			
							join('album_song as l','l.song_id=t.song_id','INNER')->
							join('album_desc as m',"m.album_id=l.album_id AND m.language_id='1'",'INNER')->
							join('genre as n','n.genre_id=p.genre_id','INNER')->
							join('loyalty_label as o','o.loyalty_label_id=q.label_id','INNER')->
							count_all_results()
		);
		echo $this->DB_LIVE->last_query();
		exit;
		$data = array();
		if(!empty($contentId)): //if content ID exist

		else:
			$dataResult = $this->DB_LIVE->select('t.song_id,t.song_name,p.song_lastupdate,k.artist_id,k.artist_name,m.album_id,m.album_name,n.genre_id,n.genre_name,o.loyalty_label_name')->
			join('song as p','p.song_id=t.song_id','INNER')->
			join('artist_song as j','j.song_id=t.song_id','INNER')->
			join('artist_desc as k',"k.artist_id=j.artist_id AND k.language_id='1'",'INNER')->
			join('artist as q','q.artist_id=k.artist_id','INNER')->			
			join('album_song as l','l.song_id=t.song_id','INNER')->
			join('album_desc as m',"m.album_id=l.album_id AND m.language_id='1'",'INNER')->
			join('genre as n','n.genre_id=p.genre_id','INNER')->
			join('loyalty_label as o','o.loyalty_label_id=q.label_id','INNER')->
			limit($limit,$offset)->
			get_where('song_desc as t',array('t.language_id' => 1))->
			result();
			foreach($dataResult as $value):
				$data[] = array(
					'contentId' => sprintf('%02d',$value->genre_id).sprintf('%06d',$value->song_id).sprintf('%06d',$value->artist_id).sprintf('%06d',$value->album_id),
					'contentTypes' => array('FS'),
					'label' => urldecode($value->loyalty_label_name),
					'genres' => array(urldecode($value->genre_name)),
					'title_th' => urldecode($value->song_name),
					'title_en' => urldecode($value->song_name),
					'artist_th' => urldecode($value->artist_name),
					'artist_en' => utf8_decode(urldecode($value->artist_name)),
					'album_th' => urldecode($value->album_name),
					'album_en' => urldecode($value->album_name),
					'pubDate' => $value->song_lastupdate,
					'moDate' => $value->song_lastupdate
				);
			endforeach;
		endif;

		/*allData Packing*/
		$totalRows = count($dataResult);
		$allData['result']['items'] = $data;
		$allData['numItems'] = $totalRows;
		$allData['moreItems'] = ($totalRows > ($limit+$offset) ? true:false);
		return $allData;
	}
	
	/*www.555.in.th request json file*/
	function www555inthExport()
	{
		/*Hero*/
		$dataSongThai = $this->memcache->get('dataHomeSongThai');
		$dataHero['songs'] = array();
		if(!empty($dataHero)) foreach($dataSongThai as $value):
			$dataArtist = array();
			foreach($value->artist as $artist):
				$dataArtist[] = array(
					'artist_name' => ''.$artist->artist_name.'',
					'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
				);
			endforeach;
			$dataHero['songs'][] = array(
				'song_external_url' => ''.$this->config->item('base_music_url').'play/'.$value->song_id.'-'.seo_url($value->song_name).'',
				'song_artist' => $dataArtist,
				'song_name' => ''.$value->song_name.'',
				'image_url' => ''.other_asset_url('uploads/album/images/original/'.$value->image->album_file_name.'.'.$value->image->album_file_ext).''
			);
		endforeach;
		
		/*Album Review*/
		$dataAlbumReview = $this->memcache->get('dataHomeAlbumReview');
		$dataTopMusicNews['title'] = 'Album Review';
		$dataTopMusicNews['albums'] = array();
		if(!empty($dataAlbumReview)) foreach($dataAlbumReview as $value):
			$dataTopMusicNews['albums'][] = array(
				'review_external_url' => ''.$this->config->item('base_news_url').'archives/'.$value->news_id.'-'.seo_url($value->news_subject).'',
				'review_subject' => $value->news_subject,
				'review_description' => $value->news_brief,
				'image_url' => ''.other_asset_url('uploads/news/images/original/'.$value->image->news_file_name.'.'.$value->image->news_file_ext).''		
			);
		endforeach;
		
		/*Music News*/
		if ( !$dataMusicNews = $this->memcache->get('dataApiMusicNews')):
			$dataMusicNews['title'] = 'Music News';
			$dataMusicNews['tab_title'] = array('Hot News','Thai','Asian','International');
			$dataMusicNews['tabs'] = array();
			
			//Music News Hot
			$dataMusicNewsHot['hot_news'] = array();
			$dataResult = $this->db
			->join('news_info as j','j.news_id=t.news_id','INNER')
			->order_by('t.views DESC,t.news_publish_date DESC')
			->get_where('news as t',"t.is_publish = '1' AND t.news_publish_date <= NOW() AND t.news_publish_date >= DATE_SUB(NOW(),INTERVAL 7 DAY)",8)
			->result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$value->image = $this->db->order_by("news_image_order ASC,news_image_id ASC")->get_where('news_image',array('news_id' => $value->news_id),1)->row();
				$dataMusicNewsHot['hot_news'][] = array(
					'news_external_url' => ''.$this->config->item('base_news_url').'archives/'.$value->news_id.'-'.seo_url($value->news_subject).'',
					'news_subject' => $value->news_subject,
					'news_description' => $value->news_brief,
					'image_url' => ''.other_asset_url('uploads/news/images/original/'.$value->image->news_file_name.'.'.$value->image->news_file_ext).''					
				);
			endforeach;
			$dataMusicNews['tabs'][] = $dataMusicNewsHot;
			
			//Music News Thai
			$dataMusicNewsThai['thai'] = array();	
			$dataResult = $this->db->select('t.news_id,t.views,t.news_publish_date,j.news_subject,j.news_brief')
			->join('news_info as j','j.news_id=t.news_id','INNER')
			->order_by('t.news_publish_date','DESC')
			->get_where('news as t',"t.news_type_id = 1 AND t.is_publish = '1' AND t.news_publish_date <= NOW()",8)
			->result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$value->image = $this->db->order_by("news_image_order ASC,news_image_id ASC")->get_where('news_image',array('news_id' => $value->news_id),1)->row();
				$dataMusicNewsThai['thai'][] = array(
					'news_external_url' => ''.$this->config->item('base_news_url').'archives/'.$value->news_id.'-'.seo_url($value->news_subject).'',
					'news_subject' => $value->news_subject,
					'news_description' => $value->news_brief,
					'image_url' => ''.other_asset_url('uploads/news/images/original/'.$value->image->news_file_name.'.'.$value->image->news_file_ext).''					
				);
			endforeach;
			$dataMusicNews['tabs'][] = $dataMusicNewsThai;
			
			//Music News Asian	
			$dataMusicNewsAsian['asian'] = array();	
			$dataResult = $this->db->select('t.news_id,t.views,t.news_publish_date,j.news_subject,j.news_brief')
			->join('news_info as j','j.news_id=t.news_id','INNER')
			->order_by('t.news_publish_date','DESC')
			->get_where('news as t',"t.news_type_id = 3 AND t.is_publish = '1' AND t.news_publish_date <= NOW()",8)
			->result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$value->image = $this->db->order_by("news_image_order ASC,news_image_id ASC")->get_where('news_image',array('news_id' => $value->news_id),1)->row();
				$dataMusicNewsAsian['asian'][] = array(
					'news_external_url' => ''.$this->config->item('base_news_url').'archives/'.$value->news_id.'-'.seo_url($value->news_subject).'',
					'news_subject' => $value->news_subject,
					'news_description' => $value->news_brief,
					'image_url' => ''.other_asset_url('uploads/news/images/original/'.$value->image->news_file_name.'.'.$value->image->news_file_ext).''					
				);
			endforeach;
			$dataMusicNews['tabs'][] = $dataMusicNewsAsian;
			
			//Music News International
			$dataMusicNewsInternational['international'] = array();	
			$dataResult = $this->db->select('t.news_id,t.views,t.news_publish_date,j.news_subject,j.news_brief')
			->join('news_info as j','j.news_id=t.news_id','INNER')
			->order_by('t.news_publish_date','DESC')
			->get_where('news as t',"t.news_type_id = 2 AND t.is_publish = '1' AND t.news_publish_date <= NOW()",8)
			->result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$value->image = $this->db->order_by("news_image_order ASC,news_image_id ASC")->get_where('news_image',array('news_id' => $value->news_id),1)->row();
				$dataMusicNewsInternational['international'][] = array(
					'news_external_url' => ''.$this->config->item('base_news_url').'archives/'.$value->news_id.'-'.seo_url($value->news_subject).'',
					'news_subject' => $value->news_subject,
					'news_description' => $value->news_brief,
					'image_url' => ''.other_asset_url('uploads/news/images/original/'.$value->image->news_file_name.'.'.$value->image->news_file_ext).''					
				);
			endforeach;
			$dataMusicNews['tabs'][] = $dataMusicNewsInternational;
			$dataMusicNews['more_url'] = $this->config->item('base_news_url');
						
			//Cache Set
			$this->memcache->set('dataApiMusicNews', $dataMusicNews, $this->config->item('memcache_time'));
		endif;
		
		/*Music Video Recommend*/
		$dataVideoHilight = $this->memcache->get('dataVideoHilight');
		$dataTopVideo['title'] = 'Music Video Recommend';
		$dataTopVideo['video'] = array();
		if(!empty($dataVideoHilight)) foreach($dataVideoHilight as $value):
			$dataArtist = array();
			if(!empty($value->artist)) foreach($value->artist as $artist):
				$dataArtist[] = array(
					'artist_name' => ''.$artist->artist_name.'',
					'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
				);
			endforeach;
			$dataTopVideo['video'][] = array(
				'video_external_url' => ''.$this->config->item('base_video_url').'play/'.$value->video_id.'-'.seo_url($value->video_name).'',
				'video_artist' => $dataArtist,
				'video_name' => ''.$value->video_name.'',
				'image_url' => ''.other_asset_url('uploads/video/images/original/'.$value->video_thumb_name.'.'.$value->video_thumb_ext).''
			);
		endforeach;
		
		/*Video*/
		$dataHotVideo['title'] = 'Video';
		$dataHotVideo['tab_title'] = array('MV','Concert','Interview','Entertainment News');
		$dataHotVideo['tabs'] = array();
			
		//Video MV
		$dataVideoMV = $this->memcache->get('dataHomeVideoMV');
		$dataMV['mv'] = array();
		if(!empty($dataVideoMV)) foreach($dataVideoMV as $value):
			$dataArtist = array();
			if(!empty($value->artist)) foreach($value->artist as $artist):
				$dataArtist[] = array(
					'artist_name' => ''.$artist->artist_name.'',
					'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
				);
			endforeach;
			$dataMV['mv'][] = array(
				'video_external_url' => ''.$this->config->item('base_video_url').'play/'.$value->video_id.'-'.seo_url($value->video_name).'',
				'video_artist' => $dataArtist,
				'video_name' => ''.$value->video_name.'',
				'image_url' => ''.other_asset_url('uploads/video/images/original/'.$value->video_thumb_name.'.'.$value->video_thumb_ext).''
			);
		endforeach;
		$dataHotVideo['tabs'][] = $dataMV;
			
		//Video Concert
		$dataVideoConcert = $this->memcache->get('dataHomeVideoConcert');
		$dataConcert['concert'] = array();
		if(!empty($dataVideoConcert)) foreach($dataVideoConcert as $value):
			$dataArtist = array();
			if(!empty($value->artist)) foreach($value->artist as $artist):
				$dataArtist[] = array(
					'artist_name' => ''.$artist->artist_name.'',
					'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
				);
			endforeach;
			$dataConcert['concert'][] = array(
				'video_external_url' => ''.$this->config->item('base_video_url').'play/'.$value->video_id.'-'.seo_url($value->video_name).'',
				'video_artist' => $dataArtist,
				'video_name' => ''.$value->video_name.'',
				'image_url' => ''.other_asset_url('uploads/video/images/original/'.$value->video_thumb_name.'.'.$value->video_thumb_ext).''
			);
		endforeach;
		$dataHotVideo['tabs'][] = $dataConcert;
			
		//Video Interview
		$dataVideoInterview = $this->memcache->get('dataHomeVideoInterview');
		$dataInterview['interview'] = array();
		if(!empty($dataVideoInterview)) foreach($dataVideoInterview as $value):
			$dataArtist = array();
			if(!empty($value->artist)) foreach($value->artist as $artist):
				$dataArtist[] = array(
					'artist_name' => ''.$artist->artist_name.'',
					'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
				);
			endforeach;
			$dataInterview['interview'][] = array(
				'video_external_url' => ''.$this->config->item('base_video_url').'play/'.$value->video_id.'-'.seo_url($value->video_name).'',
				'video_artist' => $dataArtist,
				'video_name' => ''.$value->video_name.'',
				'image_url' => ''.other_asset_url('uploads/video/images/original/'.$value->video_thumb_name.'.'.$value->video_thumb_ext).''
			);
		endforeach;
		$dataHotVideo['tabs'][] = $dataInterview;
			
		//Video Entertainment News
		$dataVideoNews = $this->memcache->get('dataHomeVideoNews');
		$dataEntertainment['entertainment_news'] = array();
		if(!empty($dataVideoNews)) foreach($dataVideoNews as $value):
			$dataArtist = array();
			if(!empty($value->artist)) foreach($value->artist as $artist):
				$dataArtist[] = array(
					'artist_name' => ''.$artist->artist_name.'',
					'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
				);
			endforeach;
			$dataEntertainment['entertainment_news'][] = array(
				'video_external_url' => ''.$this->config->item('base_video_url').'play/'.$value->video_id.'-'.seo_url($value->video_name).'',
				'video_artist' => $dataArtist,
				'video_name' => ''.$value->video_name.'',
				'image_url' => ''.other_asset_url('uploads/video/images/original/'.$value->video_thumb_name.'.'.$value->video_thumb_ext).''
			);
		endforeach;
		$dataHotVideo['tabs'][] = $dataEntertainment;
		$dataHotVideo['more_url'] = $this->config->item('base_video_url');
		
		/*Music Top Thai Song*/
		$dataMusicChartThai = $this->memcache->get('dataMusicChartThai');		
		$dataTopThaiSong['title'] = 'Top Thai Song';
		$dataTopThaiSong['songs'] = array();
		if(!empty($dataMusicChartThai)) foreach($dataMusicChartThai as $value):
			$dataArtist = array();
			if(!empty($value->artist)) foreach($value->artist as $artist):
				$dataArtist[] = array(
					'artist_name' => ''.$artist->artist_name.'',
					'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
				);
			endforeach;
			$dataTopThaiSong['songs'][] = array(
				'song_external_url' => ''.$this->config->item('base_music_url').'play/'.$value->song_id.'-'.seo_url($value->song_name).'',
				'song_artist' => $dataArtist,
				'song_name' => ''.$value->song_name.'',
				'image_url' => ''.other_asset_url('uploads/album/images/original/'.$value->album_file_name.'.'.$value->album_file_ext).''
			);
		endforeach;
		
		/*Music Thai*/
		if ( !$dataHotThaiSong = $this->memcache->get('dataApiMusicThai')):
			$dataHotThaiSong['title'] = 'Thai Song';
			$dataHotThaiSong['tab_title'] = array('New Release','Recommend','Hot','Chords');
			$dataHotThaiSong['tabs'] = array();
			
			//Music Thai News Release
			$dataMusicThaiNewsRelease['songs'] = array();
			$dataResult =  $this->db->select('t.song_id,j.song_name,t.views')
			->join('song_genre as k','k.song_id=t.song_id','INNER')
			->join('genre as l','l.genre_id = k.genre_id','INNER')			
			->join('song_info as j',"j.song_id=t.song_id",'INNER')
			->order_by('t.song_publish_date DESC,t.song_id DESC')
			->group_by('t.song_id')		
			->get_where('song as t',"t.media_type_id = 1 AND t.is_publish = '1' AND (t.song_publish_date IS NULL OR t.song_publish_date < NOW())",8)
			->result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$value->artist = $this->db->select('j.artist_id,j.artist_name')
				->join('artist_info as j','j.artist_id=t.artist_id','INNER')
				->get_where('song_artist as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'))
				->result();
					
				$value->album = $this->db->select('j.album_id,j.album_file_name,j.album_file_ext')
				->join('album_info as j','j.album_id=t.album_id','INNER')
				->get_where('song_album as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'),1)
				->row();
				
				$dataArtist = array();
				if(!empty($value->artist)) foreach($value->artist as $artist):
					$dataArtist[] = array(
						'artist_name' => ''.$artist->artist_name.'',
						'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
					);
				endforeach;
				
				$dataMusicThaiNewsRelease['songs'][] = array(
					'song_external_url' => ''.$this->config->item('base_music_url').'play/'.$value->song_id.'-'.seo_url($value->song_name).'',
					'song_artist' => $dataArtist,
					'song_name' => ''.$value->song_name.'',
					'image_url' => ''.other_asset_url('uploads/album/images/original/'.$value->album->album_file_name.'.'.$value->album->album_file_ext).''				
				);				
			endforeach;	
			$dataHotThaiSong['tabs'][] = $dataMusicThaiNewsRelease;
			
			//Music Thai Recommend
			$dataMusicThaiRecommend['songs'] = array();
			$dataResult =  $this->db->select('t.song_id,j.song_name,t.views')
			->join('song_genre as k','k.song_id=t.song_id','INNER')
			->join('genre as l','l.genre_id = k.genre_id','INNER')			
			->join('song_info as j',"j.song_id=t.song_id",'INNER')
			->order_by('t.song_publish_date DESC,t.song_id DESC')
			->group_by('t.song_id')		
			->get_where('song as t',"t.media_type_id = 1 AND is_show_recommend = '1' AND t.is_publish = '1' AND (t.song_publish_date IS NULL OR t.song_publish_date < NOW())",8)
			->result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$value->artist = $this->db->select('j.artist_id,j.artist_name')
				->join('artist_info as j','j.artist_id=t.artist_id','INNER')
				->get_where('song_artist as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'))
				->result();
					
				$value->album = $this->db->select('j.album_id,j.album_file_name,j.album_file_ext')
				->join('album_info as j','j.album_id=t.album_id','INNER')
				->get_where('song_album as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'),1)
				->row();
				
				$dataArtist = array();
				if(!empty($value->artist)) foreach($value->artist as $artist):
					$dataArtist[] = array(
						'artist_name' => ''.$artist->artist_name.'',
						'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
					);
				endforeach;
				
				$dataMusicThaiRecommend['songs'][] = array(
					'song_external_url' => ''.$this->config->item('base_music_url').'play/'.$value->song_id.'-'.seo_url($value->song_name).'',
					'song_artist' => $dataArtist,
					'song_name' => ''.$value->song_name.'',
					'image_url' => ''.other_asset_url('uploads/album/images/original/'.$value->album->album_file_name.'.'.$value->album->album_file_ext).''				
				);				
			endforeach;	
			$dataHotThaiSong['tabs'][] = $dataMusicThaiRecommend;
			
			//Music Thai Hot
			$dataMusicThaiHot['songs'] = array();
			$dataResult =  $this->db->select('t.song_id,j.song_name,t.views')
			->join('song_genre as k','k.song_id=t.song_id','INNER')
			->join('genre as l','l.genre_id = k.genre_id','INNER')			
			->join('song_info as j',"j.song_id=t.song_id",'INNER')
			->order_by('t.views DESC,t.song_publish_date DESC')
			->group_by('t.song_id')		
			->get_where('song as t',"t.media_type_id = 1 AND DATE(t.song_publish_date) >= DATE_SUB(CURDATE(),INTERVAL 1 MONTH) AND t.is_publish = '1' AND (t.song_publish_date IS NULL OR t.song_publish_date < NOW())",8)
			->result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$value->artist = $this->db->select('j.artist_id,j.artist_name')
				->join('artist_info as j','j.artist_id=t.artist_id','INNER')
				->get_where('song_artist as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'))
				->result();
					
				$value->album = $this->db->select('j.album_id,j.album_file_name,j.album_file_ext')
				->join('album_info as j','j.album_id=t.album_id','INNER')
				->get_where('song_album as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'),1)
				->row();
				
				$dataArtist = array();
				if(!empty($value->artist)) foreach($value->artist as $artist):
					$dataArtist[] = array(
						'artist_name' => ''.$artist->artist_name.'',
						'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
					);
				endforeach;
				
				$dataMusicThaiHot['songs'][] = array(
					'song_external_url' => ''.$this->config->item('base_music_url').'play/'.$value->song_id.'-'.seo_url($value->song_name).'',
					'song_artist' => $dataArtist,
					'song_name' => ''.$value->song_name.'',
					'image_url' => ''.other_asset_url('uploads/album/images/original/'.$value->album->album_file_name.'.'.$value->album->album_file_ext).''				
				);				
			endforeach;	
			$dataHotThaiSong['tabs'][] = $dataMusicThaiHot;
			
			//Chord Thai New
			$dataChordsThai = $this->memcache->get('dataChordsThai');		
			$dataChords['chord'] = array();
			if(!empty($dataChordsThai)) foreach($dataChordsThai as $value):
				$dataChords['chord'][] = array(
					'chord_external_url' => ''.$this->config->item('base_chord_url').'play/'.$value->chords_id.'-'.seo_url($value->song_name).'',
					'chord_artist' => array(
						'artist_name' => ''.$artist->artist_name.'',
						'artist_url' => ''.$this->config->item('base_chord_url').'artist/'.seo_url($artist->artist_name).''
					),
					'chord_name' => ''.$value->song_name.'',
					'image_url' => ''.other_asset_url('uploads/'.($value->song_id != NULL ? 'album':'chords').'/images/original/'.$value->album_file_name.'.'.$value->album_file_ext).''
				);
			endforeach;
			$dataHotThaiSong['tabs'][] = $dataChords;		
			$dataHotThaiSong['more_url'] = $this->config->item('base_music_url').'new_released/thai/all/';
			
			//Cache Set
			$this->memcache->set('dataApiMusicThai', $dataHotThaiSong, $this->config->item('memcache_time'));			
		endif;

		/*Music Top International Song*/
		$dataMusicChartInternational = $this->memcache->get('dataMusicChartInter');		
		$dataTopIntenationalSong['title'] = 'Top Intenational Song';
		$dataTopIntenationalSong['songs'] = array();
		if(!empty($dataMusicChartInternational)) foreach($dataMusicChartInternational as $value):
			$dataArtist = array();
			if(!empty($value->artist)) foreach($value->artist as $artist):
				$dataArtist[] = array(
					'artist_name' => ''.$artist->artist_name.'',
					'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
				);
			endforeach;
			$dataTopIntenationalSong['songs'][] = array(
				'song_external_url' => ''.$this->config->item('base_music_url').'play/'.$value->song_id.'-'.seo_url($value->song_name).'',
				'song_artist' => $dataArtist,
				'song_name' => ''.$value->song_name.'',
				'image_url' => ''.other_asset_url('uploads/album/images/original/'.$value->album_file_name.'.'.$value->album_file_ext).''
			);
		endforeach;

		/*Music Intenational*/		
		if ( !$dataHotInternationalSong = $this->memcache->get('dataApiMusicInternational')):
			$dataHotInternationalSong['title'] = 'International Song';
			$dataHotInternationalSong['tab_title'] = array('New Release','Recommend','Hot','Chords');
			$dataHotInternationalSong['tabs'] = array();
			
			//Music Intenational News Release
			$dataMusicIntenationalNewsRelease['songs'] = array();
			$dataResult =  $this->db->select('t.song_id,j.song_name,t.views')
			->join('song_genre as k','k.song_id=t.song_id','INNER')
			->join('genre as l','l.genre_id = k.genre_id','INNER')			
			->join('song_info as j',"j.song_id=t.song_id",'INNER')
			->order_by('t.song_publish_date DESC,t.song_id DESC')
			->group_by('t.song_id')		
			->get_where('song as t',"t.media_type_id = 2 AND t.is_publish = '1' AND (t.song_publish_date IS NULL OR t.song_publish_date < NOW())",8)
			->result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$value->artist = $this->db->select('j.artist_id,j.artist_name')
				->join('artist_info as j','j.artist_id=t.artist_id','INNER')
				->get_where('song_artist as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'))
				->result();
					
				$value->album = $this->db->select('j.album_id,j.album_file_name,j.album_file_ext')
				->join('album_info as j','j.album_id=t.album_id','INNER')
				->get_where('song_album as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'),1)
				->row();
				
				$dataArtist = array();
				if(!empty($value->artist)) foreach($value->artist as $artist):
					$dataArtist[] = array(
						'artist_name' => ''.$artist->artist_name.'',
						'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
					);
				endforeach;
				
				$dataMusicIntenationalNewsRelease['songs'][] = array(
					'song_external_url' => ''.$this->config->item('base_music_url').'play/'.$value->song_id.'-'.seo_url($value->song_name).'',
					'song_artist' => $dataArtist,
					'song_name' => ''.$value->song_name.'',
					'image_url' => ''.other_asset_url('uploads/album/images/original/'.$value->album->album_file_name.'.'.$value->album->album_file_ext).''				
				);				
			endforeach;	
			$dataHotInternationalSong['tabs'][] = $dataMusicIntenationalNewsRelease;
			
			//Music Intenational Recommend
			$dataMusicIntenationalRecommend['songs'] = array();
			$dataResult =  $this->db->select('t.song_id,j.song_name,t.views')
			->join('song_genre as k','k.song_id=t.song_id','INNER')
			->join('genre as l','l.genre_id = k.genre_id','INNER')			
			->join('song_info as j',"j.song_id=t.song_id",'INNER')
			->order_by('t.song_publish_date DESC,t.song_id DESC')
			->group_by('t.song_id')		
			->get_where('song as t',"t.media_type_id = 2 AND is_show_recommend = '1' AND t.is_publish = '1' AND (t.song_publish_date IS NULL OR t.song_publish_date < NOW())",8)
			->result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$value->artist = $this->db->select('j.artist_id,j.artist_name')
				->join('artist_info as j','j.artist_id=t.artist_id','INNER')
				->get_where('song_artist as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'))
				->result();
					
				$value->album = $this->db->select('j.album_id,j.album_file_name,j.album_file_ext')
				->join('album_info as j','j.album_id=t.album_id','INNER')
				->get_where('song_album as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'),1)
				->row();
				
				$dataArtist = array();
				if(!empty($value->artist)) foreach($value->artist as $artist):
					$dataArtist[] = array(
						'artist_name' => ''.$artist->artist_name.'',
						'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
					);
				endforeach;
				
				$dataMusicIntenationalRecommend['songs'][] = array(
					'song_external_url' => ''.$this->config->item('base_music_url').'play/'.$value->song_id.'-'.seo_url($value->song_name).'',
					'song_artist' => $dataArtist,
					'song_name' => ''.$value->song_name.'',
					'image_url' => ''.other_asset_url('uploads/album/images/original/'.$value->album->album_file_name.'.'.$value->album->album_file_ext).''				
				);				
			endforeach;	
			$dataHotInternationalSong['tabs'][] = $dataMusicIntenationalRecommend;
			
			//Music Intenational Hot
			$dataMusicIntenationalHot['songs'] = array();
			$dataResult =  $this->db->select('t.song_id,j.song_name,t.views')
			->join('song_genre as k','k.song_id=t.song_id','INNER')
			->join('genre as l','l.genre_id = k.genre_id','INNER')			
			->join('song_info as j',"j.song_id=t.song_id",'INNER')
			->order_by('t.views DESC,t.song_publish_date DESC')
			->group_by('t.song_id')		
			->get_where('song as t',"t.media_type_id = 2 AND DATE(t.song_publish_date) >= DATE_SUB(CURDATE(),INTERVAL 1 MONTH) AND t.is_publish = '1' AND (t.song_publish_date IS NULL OR t.song_publish_date < NOW())",8)
			->result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$value->artist = $this->db->select('j.artist_id,j.artist_name')
				->join('artist_info as j','j.artist_id=t.artist_id','INNER')
				->get_where('song_artist as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'))
				->result();
					
				$value->album = $this->db->select('j.album_id,j.album_file_name,j.album_file_ext')
				->join('album_info as j','j.album_id=t.album_id','INNER')
				->get_where('song_album as t',array('t.song_id' => $value->song_id,'t.is_default' => '1'),1)
				->row();
				
				$dataArtist = array();
				if(!empty($value->artist)) foreach($value->artist as $artist):
					$dataArtist[] = array(
						'artist_name' => ''.$artist->artist_name.'',
						'artist_url' => ''.$this->config->item('base_artist_url').'summary/'.$artist->artist_id.'-'.seo_url($artist->artist_name).''
					);
				endforeach;
				
				$dataMusicIntenationalHot['songs'][] = array(
					'song_external_url' => ''.$this->config->item('base_music_url').'play/'.$value->song_id.'-'.seo_url($value->song_name).'',
					'song_artist' => $dataArtist,
					'song_name' => ''.$value->song_name.'',
					'image_url' => ''.other_asset_url('uploads/album/images/original/'.$value->album->album_file_name.'.'.$value->album->album_file_ext).''				
				);				
			endforeach;	
			$dataHotInternationalSong['tabs'][] = $dataMusicIntenationalHot;
			
			//Chord Intenational New
			$dataChordsIntenational = $this->memcache->get('dataChordsInter');		
			$dataChords['chord'] = array();
			if(!empty($dataChordsIntenational)) foreach($dataChordsIntenational as $value):
				$dataChords['chord'][] = array(
					'chord_external_url' => ''.$this->config->item('base_chord_url').'play/'.$value->chords_id.'-'.seo_url($value->song_name).'',
					'chord_artist' => array(
						'artist_name' => ''.$artist->artist_name.'',
						'artist_url' => ''.$this->config->item('base_chord_url').'artist/'.seo_url($artist->artist_name).''
					),
					'chord_name' => ''.$value->song_name.'',
					'image_url' => ''.other_asset_url('uploads/'.($value->song_id != NULL ? 'album':'chords').'/images/original/'.$value->album_file_name.'.'.$value->album_file_ext).''
				);
			endforeach;
			$dataHotInternationalSong['tabs'][] = $dataChords;		
			$dataHotInternationalSong['more_url'] = $this->config->item('base_music_url').'new_released/inter/all/';
			
			//Cache Set
			$this->memcache->set('dataApiMusicInternational', $dataHotInternationalSong, $this->config->item('memcache_time'));			
		endif;
		
		/*Artist*/
		if ( !$dataArtistTopChart = $this->memcache->get('dataApiArtistTopChart')):
			$dataArtistTopChart['title'] = 'Hot Artist';
			$dataArtistTopChart['artist'] = array();

			$dataResult = $this->db->select('t.artist_id,k.artist_name')->
			join('artist as j',"j.artist_id=t.artist_id",'INNER')->
			join('artist_info as k','k.artist_id=t.artist_id','INNER')->
			order_by('views DESC')->
			group_by('t.artist_id')->
			get_where('artist_click_stat as t',"t.update_date <= NOW() AND t.update_date >= DATE_SUB(NOW(),INTERVAL 7 DAY)",40)->
			result();
			if(!empty($dataResult)) foreach($dataResult as $value):
				$dataArtistImage = $this->db
				->order_by("artist_image_order ASC,artist_image_id ASC")
				->get_where('artist_image',array('artist_id' => $value->artist_id),1)
				->row();
				$dataArtistTopChart['artist'][] = array(
					'artist_albums_url' => ''.$this->config->item('base_artist_url').'summary/'.$value->artist_id.'-'.seo_url($value->artist_name).'',
					'artist_name' => $value->artist_name,
					'image_url' => ''.other_asset_url('uploads/artist/images/original/'.$dataArtistImage->artist_file_name.'.'.$dataArtistImage->artist_file_ext).''
				);
			endforeach;
			//Cache Set
			$this->memcache->set('dataApiArtistTopChart', $dataArtistTopChart, $this->config->item('memcache_time'));			
		endif;	
		
		//Convert To Json
		$dataJson = array(
			'hero' => $dataHero,
			'top_music_news' => $dataTopMusicNews,
			'hot_music_news' => $dataMusicNews,
			'top_video' => $dataTopVideo,
			'hot_video' => $dataHotVideo,
			'top_thai_song' => $dataTopThaiSong,
			'hot_thai_song' => $dataHotThaiSong,
			'top_intl_song' => $dataTopIntenationalSong,
			'hot_intl_song' => $dataHotInternationalSong,
			'artists' => $dataArtistTopChart
		);
		
		//Output Json File
		header('Access-Control-Allow-Origin: *');
		header('Cache-Control: no-cache, must-revalidate');
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
		header('Content-type: application/json; charset=utf-8');
		ini_set('display_errors', 1);
		echo jsonpp(json_encode($dataJson));
	}
}

?>